/*
 * sp005.cpp
 *
 *  Created on: 2012-11-26
 *      Author: feng
 */

#include "sample.h"

void CSp005::InitOpenGL() {
	this->InitPoints();

	glEnable(GL_DEPTH_TEST);	// Hidden surface removal
	glEnable(GL_CULL_FACE);		// Do not calculate inside of jet
	glFrontFace(GL_CCW);		// Counter clock-wise polygons face out

	// Nice light blue
	glClearColor(0.0f, 0.0f, 0.5f, 1.0f);

	GLfloat aAmbientLight[] = {0.3f,0.3f,0.3f,1.0f};
	GLfloat aDiffuseLight[] = {0.7f,0.7f,0.7f,1.0f};
	GLfloat aSpecular[] = {1.0f,1.0f,1.0f,1.0f};
	glEnable(GL_LIGHTING);
	glLightfv(GL_LIGHT0,GL_AMBIENT,aAmbientLight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,aDiffuseLight);
	glLightfv(GL_LIGHT0,GL_SPECULAR,aSpecular);
	glEnable(GL_LIGHT0);

	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
	GLfloat specref[] = {1.0f,1.0f,1.0f,1.0f};
	glMaterialfv(GL_FRONT,GL_SPECULAR,specref);
	glMateriali(GL_FRONT,GL_SHININESS,128);

	glEnable(GL_NORMALIZE);
}

void CSp005::InitPoints() {
	M3DVector3f temp[] = {
			// Head of the Plane///////////////////////////////
			{0.0f, 0.0f, 60.0f},{-15.0f, 0.0f, 30.0f},{15.0f, 0.0f, 30.0f},
			{15.0f, 0.0f, 30.0f},{0.0f, 15.0f, 30.0f},{0.0f, 0.0f, 60.0f},
			{0.0f, 0.0f, 60.0f},{0.0f, 15.0f, 30.0f},{-15.0f, 0.0f, 30.0f},
			// Body of the Plane///////////////////////////////
			{-15.0f, 0.0f, 30.0f},{0.0f, 15.0f, 30.0f},{0.0f, 0.0f, -56.0f},
			{0.0f, 0.0f, -56.0f},{0.0f, 15.0f, 30.0f},{15.0f, 0.0f, 30.0f},
			{15.0f, 0.0f, 30.0f},{-15.0f, 0.0f, 30.0f},{0.0f, 0.0f, -56.0f},
			// Left wing
			// Large triangle for bottom of wing
			{0.0f, 2.0f, 27.0f},{-60.0f, 2.0f, -8.0f},{60.0f, 2.0f, -8.0f},
			{60.0f, 2.0f, -8.0f},{0.0f, 7.0f, -8.0f},{0.0f, 2.0f, 27.0f},
			{60.0f, 2.0f, -8.0f},{-60.0f, 2.0f, -8.0f},{0.0f, 7.0f, -8.0f},
			// Other wing top section
			{0.0f, 2.0f, 27.0f},{0.0f, 7.0f, -8.0f},{-60.0f, 2.0f, -8.0f},
			// Tail section///////////////////////////////
			// Bottom of back fin
			{-30.0f, -0.50f, -57.0f},{30.0f, -0.50f, -57.0f},{0.0f, -0.50f, -40.0f},
			// top of left side
			{0.0f, -0.5f, -40.0f},{30.0f, -0.5f, -57.0f},{0.0f, 4.0f, -57.0f},
			// top of right side
			{0.0f, 4.0f, -57.0f},{-30.0f, -0.5f, -57.0f},{0.0f, -0.5f, -40.0f},
			// back of bottom of tail
			{30.0f, -0.5f, -57.0f},{-30.0f, -0.5f, -57.0f},{0.0f, 4.0f, -57.0f},
			// Top of Tail section left
			{0.0f, 0.5f, -40.0f},{3.0f, 0.5f, -57.0f},{0.0f, 25.0f, -65.0f},
			{0.0f, 25.0f, -65.0f},{-3.0f, 0.5f, -57.0f},{0.0f, 0.5f, -40.0f},
			// Back of horizontal section
			{3.0f, 0.5f, -57.0f},{-3.0f, 0.5f, -57.0f},{0.0f, 25.0f, -65.0f},
	};
	for (int i = 0; i < 17 * 3 ; i++) {
		m3dCopyVector3(m_TriPoints[i],temp[i]);
	}

}

void CSp005::RenderOnce() {
	GLfloat lightpos[] = {-50.0f,50.0f,50.0f,1.0f};
	glPushMatrix();
	glTranslatef(lightpos[0],lightpos[1],lightpos[2]);
	this->MySphere(10.0f,10,10);
	glPopMatrix();
	this->MyXYZ();

	glColor3ub(128, 128, 128);
	glBegin(GL_TRIANGLES);
	for (int i = 0; i < 17 * 3 ; i+=3) {
		GLfloat *p1,*p2,*p3;
		p1 = m_TriPoints[i];
		p2 = m_TriPoints[i+1];
		p3 = m_TriPoints[i+2];
		M3DVector3f vNoraml;
		m3dFindNormal(vNoraml,p1,p2,p3);
		glVertex3fv(p1);
		glVertex3fv(p2);
		glVertex3fv(p3);
		}
	glEnd(); // Of Jet

}

void CSp005::OnEventWndResize(int w,int h) {
	CSample::OnEventWndResize(w,h);
	GLfloat lightpos[] = {-50.0f,50.0f,50.0f,1.0f};
	glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
}
